Ask a Question
Send an email to me.
 
Java Question and Answer Forums
JavaRanch Big Moose Saloon
Marcus Green's Discussion Forum
java.sun.com Forums, Chat and User Groups
Artima Java Answers Forum
 
Other Resources
Java Language Specification
Java Virtual Machine Specification
Java 2 Platform, Standard Edition, v 1.4.0 API Specification
 
Tutorials
Learning the Java Language
Operator Precedence Chart, Expressions, Statements, Blocks
Programming with Assertions
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1b  2  While the reference to an instance method is resolved at run-time based on the type of the referenced object; the reference to a static method is resolved at compile-time based on the type of the reference. Suppose the existence of an abstract superclass A and a concrete subclass C. Suppose that a reference, r, of type A refers to a concrete instance of type C. If r is used to invoke an instance method then the reference will resolve to the overriding implemenation in the subclass C. The same is not true for static methods. Since the reference to a static method is resolved at compile-time based on the type of the reference, the actual method referenced will be the static method in the superclass and not the subclass method. If a method could be declared both abstract and static, then the actual method referenced at run-time could be the abstract superclass method that provides no implementation. A nested class, however, can be declared both abstract and static because the abstract nested class can be extended by another class.  
2c  Compile-time error.  Semicolons are needed at the end of the declarations of the anonymous classes.  
3c  d  e  f  g  h  i  3  4  5  6  7  8  9  The following identifiers can not be applied to a top level class: private, protected, static, synchronized, transient, volatile. The synchronized modifier can not be applied to a class because it is a method modifier. The modifiers, transient and volatile, can not be applied to any class; because they are field modifiers.  
4a  Prints: 5  The argument in the class instance creation expression is passed to the constructor of the superclass. An anonymous class can not declare a constructor, but it can declare an instance initializer. In this case, the instance initializer invokes the incY method.  
5b  Prints: 3122  The arguments in the class instance creation expression are passed to the constructor of the superclass.  
6b  c  d  2  3  4  Synchronized is a method modifier but is not a class modifier. Any attempt to declare a synchronized class will fail. Since the synchronized modifier specifies an implementation detail it makes no sense to use it with an abstract method that provides no implementation.  
7a  c  e  A local class is declared within a block.  A local class is a nested class.  A local class is always a named class.  Every class declared within the body of another class is known as a nested class. If the nested class does not have a name then it is an anonymous class. If the nested class has a name then it is not anonymous. If the nested class has a name and is declared inside of a block such as the body of a method, constructor, initializer, or statement then it is a local class. If a nested class does not have a name then it can not be called a local class even if it is declared inside of a block. Therefore, an anonymous class is never called a local class. If the nested class has a name and is not declared inside of a block then it is a member class. If the class is not nested then it is a top level class. Chapter 8 of the Java Language Specification defines class declarations. Section 8.1.2 of the JLS defines inner classes. Section 8.5 of the JLS defines member classes. Section 14.3 of the JLS defines local class declarations. Section 15.9.5 of the JLS defines anonymous class declarations.  
8e  None of the Above  A method, field, and a nested class can share the same name because they are used in different contexts and use different lookup procedures. Please note, however, that class names usually begin with an upper case letter while method and field names usually begin with a lower case letter. Also note that a nested class can not share the same name with its enclosing class. However, a method and field can share a name with the enclosing class. Even so, it not a good idea to name a method with the name of the enclosing class because it could be confused with a constructor.  
9c  Compile-time error.  An anonymous class can extend a superclass or it can implement an interface. However, an anonymous class declaration that extends a superclass is not permitted to have an implements clause.  
10c  e  3  5  Local method variables and method parameters are stored on the stack and go out of scope after the method is exited. Although a local reference to an object is also stored on the stack, the object itself is stored on the heap. If the reference to the local object is maintained outside of the method then the locally created object can continue to exist long after the method returns. Since the local or anonymous object can continue to exist longer than local method variables, the local or anonymous object is not permitted to access local method variables unless the variable is declared final and is assigned a value before the declaration of the local or anonymous class. The JVM can place a copy of the value of the final variable directly into the local or anonymous class so there is no further dependence on the method variable.  
11d  f  g  abstract  final  strictfp  A local class is a named class that is declared inside of a block. No access modifier can be applied to a local class declaration. A local class can not be static. A local class can be abstract and can be final. The strictfp modifier can be applied to a class, interface, or method to guarantee that all float or double operations are processed according to the IEEE 754 standard. Without the strictfp modifier the float or double operations can be processed using the greatest precision available. For example, a 64 bit CPU could process intermediate float operations using 64 rather than only 32 bits. If a class is marked strictfp then all float or double expressions within all nested classes, methods, constructors, static initializers, instance initializers, and variable initializers will also be strictfp.  
12f  Compiler Error  Class Z is a non-static member class of class D. Since Z is non-static, an instance of Z can not be instantiated without first creating an instance of class Z. Therefore, the correct syntax for creating an instance of class Z would be as follows: "new D().new Z()".  
13b  Prints: Z  Class Z is a static member class of class E. Static member classes are similar to ordinary top-level classes with the added advantage of having access to all of the static fields and methods (including those that are private) of the enclosing class. Since Z can be instantiated without first creating an instance of the enclosing class, only the letter "Z" is printed.  
14d  An anonymous class is implicitly final.  An anonymous class can extend another class or implement an interface. If the anonymous class extends another class then the class instance creation expression can include parameters. The parameters will be passed to a constructor of the super class. If the anonymous class implements an interface, then the instance creation expression can not include any parameters. An anonymous class can not be extended so it also can not be abstract. An anonymous class can not be static. An anonymous class can not declare a constructor, but it can declare an instance initializer.  
15e  Prints: Z.m1 Y.m1  Member classes have access to the private fields and methods of the enclosing class and other member classes. Furthermore, the enclosing class has access to the private fields and methods of the member classes.  
16a  b  c  e  s1   s2   s3   s5   Class Z is a local class defined within the code block of method m1 of class C. Class Z therefore has access to all fields and methods (static, non-static, and private) of class C. Additionally, class Z has access to the local variables and parameters of method m1 that are declared final. Consequently, class Z does not have access to parameter s6 or local variable s4. A local variable lives on the stack and disappears as soon as the method is exited. A local class, however, might continue to exist even after the method has been exited. For that reason, a local class can not access local variables that might have a short life cycle than the local class.  
17g  Compiler error at line 8.  A static nested class does not have access to non-static members of the enclosing class.  

Copyright © 2002, Dan Chisholm
All rights reserved.
1